package com.lixh.roommanager.ui.main

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.fragment.app.Fragment
import com.github.salomonbrys.kotson.fromJson
import com.google.gson.Gson
import com.lixh.roommanager.R
import com.lixh.roommanager.ui.Constant
import com.lixh.roommanager.ui.bean.Room
import com.lixh.roommanager.ui.utils.UStore
import com.lixh.roommanager.ui.utils.toast
import kotlinx.android.synthetic.main.fragment_set.*
import kotlinx.android.synthetic.main.fragment_set.view.*

/**
 * A placeholder fragment containing a simple view.
 */
class SetFragment : Fragment() {
    var room by UStore(Constant.JSON_ROOM, "[]")
    private val rooms: ArrayList<Room> = arrayListOf()
    private var result: String? = null
    lateinit var currentView: View
    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.fragment_set, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        this.currentView = view
        view.tv_submit.setOnClickListener {
            val newRoom =
                Gson().fromJson<ArrayList<Room>>(room)
            rooms.clear()
            rooms.addAll(newRoom)
            makeData(view)
            room = result.toString()
            toast("添加成功")
        }
        //精确度
        tv_precise.setOnClickListener {
            //获取精确 格式化
            val type = Constant.TIME_TYPE
            Constant.TIME_TYPE = when (type) {
                0 -> {
                    tv_precise.text="切换至天"
                    toast("已切换至精准分秒")
                    1
                };
                else -> {
                    tv_precise.text="切换至精准分秒"
                    toast("已切换至天")
                    0
                };
            }
        }
        tv_clear.setOnClickListener {
            view.et_room.setText("")
            //测试用
//            room = "[]"
        }
    }

    private fun makeData(view: View) {
        val roomStr: String = view.et_room.text.toString()
        if (roomStr.isEmpty()) {
            Toast.makeText(context, "房间开始必须填写", Toast.LENGTH_SHORT).show()
        } else {
            val roomStartI: List<String> = roomStr.split(";")
//如果有结尾 则 循环批量生成
            if (roomStartI.size > 1) {
                for (roomName in roomStartI) {
                    if (checkRoom(roomName)) {
                        val room = Room(roomName)
                        rooms.add(room)
                    }
                }
            } else {
                //否则 只有开始 直接添加
                if (checkRoom(roomStartI[0])) {
                    val room = Room(roomStartI[0])
                    rooms.add(room)
                }
            }
        }
        result = Gson().toJson(rooms)
    }

    //检查是否有重复
    private fun checkRoom(name: String): Boolean {
        return rooms.none { it.roomName == name }
    }

    companion object {
        /**
         * The fragment argument representing the section number for this
         * fragment.
         */
        private const val ARG_SECTION_NUMBER = "section_number"

        /**
         * Returns a new instance of this fragment for the given section
         * number.
         */
        @JvmStatic
        fun newInstance(): SetFragment {
            return SetFragment()
        }
    }
}